package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.dao.ResComment;
import com.gxnzd.communityelderly.dao.ResCriticizeList;
import com.gxnzd.communityelderly.entity.Criticize;
import com.gxnzd.communityelderly.entity.Employee;
import com.gxnzd.communityelderly.entity.Forum;
import com.gxnzd.communityelderly.entity.PostLike;
import com.gxnzd.communityelderly.mapper.EmployeeMapper;
import com.gxnzd.communityelderly.mapper.ForumMapper;
import com.gxnzd.communityelderly.mapper.PostLikeMapper;
import com.gxnzd.communityelderly.service.CriticizeService;
import com.gxnzd.communityelderly.mapper.CriticizeMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 23234
* @description 针对表【criticize】的数据库操作Service实现
* @createDate 2024-11-15 23:39:54
*/
@Service
public class CriticizeServiceImpl extends ServiceImpl<CriticizeMapper, Criticize>
    implements CriticizeService{

    @Resource
    private CriticizeMapper criticizeMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private ForumMapper forumMapper;

    @Resource
    private PostLikeMapper postLikeMapper;

    /**
     * 添加评论
     * @param criticize
     * @return
     */
    @Override
    public R addCriticize(Criticize criticize) {
        criticize.setCriticizeLike(0);
        //校验帖子id和用户id是否存在
        Employee employee = employeeMapper.selectById(criticize.getUserId());
        if(employee ==null){
            return R.error("当前用户不存在!");
        }
        Forum forum = forumMapper.selectById(criticize.getPostId());
        if(forum == null){
            return R.error("当前评论的帖子不存在！");
        }
        //添加评论
        criticizeMapper.insert(criticize);
        //对帖子的评论数量进行更改
        forum.setCriticizeNum(forum.getCriticizeNum()+1);
        forumMapper.updateById(forum);
        return R.success("评论成功!");
    }


    /**
     * 获取评论列表
     * @param page
     * @param pageSize
     * @param criticizeContent
     * @return
     */
    @Override
    public R getCriticizeList(Integer page, Integer pageSize, String criticizeContent,Integer userId) {
        //得到帖子模糊查询的数据然后循环遍历
        Page<Criticize> criticizePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Criticize> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(criticizeContent),Criticize::getCriticizeContent,criticizeContent)
                .orderByDesc(Criticize::getCreateTime);
        criticizeMapper.selectPage(criticizePage,queryWrapper);
        Page<ResComment> commentPage = new Page<>();
        ArrayList<ResComment> comments = new ArrayList<>();
        for (Criticize criticize:criticizePage.getRecords()) {
            ResComment resComment = new ResComment();
            //查帖子
            Forum forum = forumMapper.selectById(criticize.getPostId());
            //查发言人
            Employee employee = employeeMapper.selectById(criticize.getUserId());
            resComment.setCriticize(criticize);
            resComment.setForum(forum);
            resComment.setEmployee(employee);
            //查询用户是否点赞了这条评论
            LambdaQueryWrapper<PostLike> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PostLike::getCriticizeId,criticize.getCriticizeId())
                            .eq(PostLike::getUserId,userId)
                            .eq(PostLike::getType,2);
            PostLike like = postLikeMapper.selectOne(wrapper);
            if(like == null){
                resComment.setLike(false);
            }else{
                resComment.setLike(true);
            }
            comments.add(resComment);
        }
        commentPage.setRecords(comments);
        commentPage.setPages(criticizePage.getPages());
        commentPage.setSize(criticizePage.getSize());
        commentPage.setTotal(criticizePage.getTotal());
        commentPage.setCurrent(criticizePage.getCurrent());
        return R.success(commentPage);
    }

    /**
     * 删除评论
     * @param criticizeId
     * @return
     */
    @Override
    public R deleteCriticizeInfo(Long criticizeId) {
        //对帖子的评论数量进行删除
        Criticize criticize = criticizeMapper.selectById(criticizeId);
        Forum forum = forumMapper.selectById(criticize.getPostId());
        forum.setCriticizeNum(forum.getCriticizeNum()-1);
        forumMapper.updateById(forum);
        criticizeMapper.deleteById(criticizeId);
        //删除点赞表中所有关于这条评论的数据
        LambdaQueryWrapper<PostLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostLike::getCriticizeId,criticizeId);
        postLikeMapper.delete(wrapper);
        return R.success("删除成功");
    }


    /**
     * 获取某个帖子所有评论以及某个用户是否赞了评论
     * @param postId
     * @param userId
     * @return
     */
    @Override
    public R getPostCriticizeList(Integer postId, Integer userId) {
        ArrayList<ResCriticizeList> lists = new ArrayList<>();
        LambdaQueryWrapper<Criticize> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Criticize::getPostId,postId);
        for (Criticize criticize : criticizeMapper.selectList(queryWrapper)) {
            ResCriticizeList resCriticize = new ResCriticizeList();
            resCriticize.setCriticize(criticize);
            //查询评论人的信息
            Employee employee = employeeMapper.selectById(criticize.getUserId());
            resCriticize.setUser(employee);
            //查询喜欢表里是否有该用户的点赞记录
            LambdaQueryWrapper<PostLike> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PostLike::getCriticizeId,criticize.getCriticizeId())
                    .eq(PostLike::getUserId,userId)
                    .eq(PostLike::getType,2);
            PostLike like = postLikeMapper.selectOne(wrapper);
            if(like ==null){
                resCriticize.setLike(false);
            }else{
                resCriticize.setLike(true);
            }
            lists.add(resCriticize);
        }
        return R.success(lists);
    }
}




